package it.yuzuojian.com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.yuzuojian.com.domain.CommonResult;
import it.yuzuojian.com.domain.Consumer;
import it.yuzuojian.com.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @PostMapping("/delete")
    public CommonResult deleteMoreConsumer(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(requestJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "json后端转换失败！", false);
        }
        JsonNode ids = jsonNode.get("ids");
        String[] split = ids.toString().replace("[", "").replace("]", "")
                .trim().split(",");
        Integer[] data = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            data[i] = Integer.parseInt(split[i]);
        }
        if (consumerService.delete(data)) {
            return new CommonResult<Boolean>(1, "删除成功！", true);
        }
        return new CommonResult<Boolean>(0, "该用户不存在！", false);
    }

    @PostMapping("/update")
    public CommonResult updateConsumer(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        Consumer consumer;
        try {
            consumer = objectMapper.readValue(requestJson, Consumer.class);
            consumer.setUpdateTime(new Date());
            System.out.println(consumer);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "json后端转换失败！", false);
        }
        if (consumerService.update(consumer)) {
            return new CommonResult<Boolean>(1, "修改成功！", true);
        }
        return new CommonResult<Boolean>(0, "修改失败！", false);
    }

    @PostMapping("/add")
    public CommonResult addConsumer(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        Consumer consumer;
        try {
            consumer = objectMapper.readValue(requestJson, Consumer.class);
            consumer.setCreateTime(new Date());
            consumer.setUpdateTime(new Date());
            System.out.println(consumer);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "json后端转换失败！", false);
        }
        if (consumerService.insert(consumer)) {
            return new CommonResult<Boolean>(1, "添加成功！", true);
        }
        return new CommonResult<Boolean>(0, "添加失败！", false);
    }

    @GetMapping("/findByUsername")
    public CommonResult findByUsername(String username) {
        List<Consumer> consumers = null;
        System.out.println(username);
        try {
            consumers = consumerService.selectByUsername(username);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<Consumer>>(0, "查询失败！", consumers);
        }
        return new CommonResult<List<Consumer>>(1, "查询成功！", consumers);
    }

    @GetMapping("/findOneByUsername")
    public CommonResult findOneByUsername(String username) {
        Consumer consumer = null;
        System.out.println(username);
        try {
            consumer = consumerService.selectOneByUsername(username);
            if (consumer == null) {
                return new CommonResult<Consumer>(0, "不存在该用户！", consumer);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<Consumer>(0, "查询失败！", consumer);
        }
        return new CommonResult<Consumer>(1, "查询成功！", consumer);
    }

    @GetMapping("/findAll")
    public CommonResult findAll() {
        List<Consumer> consumers = null;
        try {
            consumers = consumerService.selectAll();
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<Consumer>>(0, "查询失败！", consumers);
        }
        return new CommonResult<List<Consumer>>(1, "查询成功！", consumers);
    }

    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        Consumer consumer = null;
        System.out.println(id);
        try {
            consumer = consumerService.selectById(id);
            System.out.println(consumer);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<Consumer>(0, "查询失败！", consumer);
        }
        return new CommonResult<Consumer>(1, "查询成功！", consumer);
    }

    @PostMapping("/updateConsumerPic")
    public CommonResult updateConsumerPic(MultipartFile file, Integer id) throws IOException {
        if (file == null || file.isEmpty()) {
            return new CommonResult<Boolean>(0, "上传文件为空", false);
        }
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "music"
                + System.getProperty("file.separator") + "img" + System.getProperty("file.separator")
                + "consumerPic"/*+System.getProperty("file.separator")*/;
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdir();
        }
        File upload = new File(path, fileName);
        System.out.println("path = " + path);
        System.out.println("file:" + upload.getCanonicalPath());
        String pic = "/img/consumerPic/" + fileName;
        try {
            file.transferTo(upload);
            Consumer consumer = new Consumer();
            consumer.setId(id);
            consumer.setAvator(pic);
            consumerService.update(consumer);
        } catch (IOException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "文件上传失败", false);
        }
        return new CommonResult<Boolean>(1, "文件上传成功", true);

    }



}
